<?php


namespace app\admin\service;

use app\admin\model\CustomerModel;
use app\admin\model\FenPeiModel;
use think\Db;

/**
 * Class CustomerService
 * @package app\admin\service
 * 客户服务
 */
class CustomerService{
    /**
     * 查询分配表中的分配
     */
    public function getCustmoers($page,$size){
        //查询的所有分配表
        $fenPeiModel = NEW FenPeiModel();
        $fenpPeiList = $fenPeiModel->limit(($page-1)*$size,$size)->order('id','desc')->select();

        //获取用户分配的管家信息
        $customers = array();
        foreach ($fenpPeiList as $key=>$fenPei){
            //查询的是用户信息
            $customer = CustomerModel::get(["id","=",$fenPei->user_id]);
            //查询管家的用户信息
            $guanJia = CustomerModel::get(["id","=",$fenPei->guan_jia_id]);
            //查询管理员信息
            $admin = CustomerModel::get(["id","=",$fenPei->admin_id]);
            $customer["guan_jia_id"] = $guanJia['id'];
            $customer["guan_jia_name"] = $guanJia['name'];
            $customer["guan_jia_username"] = $guanJia['username'];
            $customer["admin_id"] = $admin['id'];
            $customer["admin_name"] = $admin['name'];
            $customer["admin_useranme"] = $admin['username'];
            array_push($customers,$customer);
        }
        return $customers;
    }

    /**
     * 获得总条数
     */
    public function getCount(){
        $fenPeiModel = NEW FenPeiModel();
        $count = $fenPeiModel->count("id");
        return $count;
    }

    /**
     * 获取所有的管理员
     */
    public function getGuanLiYuanAll(){
        $customerModel = new CustomerModel();
        $guanLiYuans = $customerModel->where("role","=","3")->select();
        return $guanLiYuans;
    }

    /**
     * 获取所有的管加
     */
    public function getGuanJiaAll(){
        $customerModel = new CustomerModel();
        $guanJias = $customerModel->where("role","=","1")->select();
        return $guanJias;
    }

    /**
     * 查询未分配的用户
     */
    public function getWeiFenPeiUsers($page,$size){
        $result = Db::query('select * from chat_chat_user as u LEFT JOIN chat_fen_pei as f on u.id = f.user_id WHERE ISNULL(f.user_id) limit '.($page-1)*$size.','.$size);
        return $result;
    }

    /**
     * 获取所有的用户
     */
    public function getUsers($page,$size){
        $customerModel = new CustomerModel();
        $users = $customerModel->limit(($page-1)*$size,$size)->select();
        return $users;
    }

    /**
     *获取用户总条数
     */
    public  function getUserCount(){
        $customerModel = new CustomerModel();
        $count = $customerModel->count("id");
        return $count;
    }
}